package com.mobilesales.android.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class SelectDateTime {

	private Calendar lich = Calendar.getInstance();

	private Context context;

	public SelectDateTime(Context context) {
		this.context = context;

	}

	public void my_time_dialog_ngay_den(final EditText dateField) {
		DatePickerDialog dpdialog = new DatePickerDialog(context,
				new OnDateSetListener() {
					// Calendar lich = Calendar.getInstance();

					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {

						// TODO Auto-generated method stub
						dateField.setText(year + "-" + (monthOfYear + 1) + "-"
								+ dayOfMonth);
					}
				}, lich.get(Calendar.YEAR), lich.get(Calendar.MONTH),
				lich.get(Calendar.DAY_OF_MONTH));
		dpdialog.show();
	}

	public void showTimePickerDialog(final EditText textf) {
		OnTimeSetListener callback = new OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				String s = hourOfDay + ":" + minute;
				textf.setText(s);
			}
		};
		String strArr[] = textf.getText().toString().split(":");
		int gio = Integer.parseInt(strArr[0]);
		int phut = Integer.parseInt(strArr[1]);
		TimePickerDialog time = new TimePickerDialog(context, callback, gio,
				phut, true);
		time.show();
	}

	public String FormatDate(String sdate) {
		String tdate = "";
		SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy");
		try {
			Date date = simple.parse(sdate);
			return new SimpleDateFormat("yyyy-MM-dd").format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tdate;
	}

	public String FormatDate(Date date) {
		String tdate = "";
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
		tdate = simple.format(date);
		return tdate;
	}

	public Date getDate(String sdate) {
		SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy");
		try {
			return simple.parse(sdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
